///新版本
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_ahj_app/app/NewMine/AccountViewModel.dart';
import 'package:flutter_ahj_app/base/cunchu/UserInfoUtils.dart';
import 'package:flutter_ahj_app/base/utils/JUIKit/JText.dart';
import 'package:flutter_ahj_app/base/utils/ToastUtils.dart';
import 'package:flutter_ahj_app/base/values/SQColor.dart';
import 'package:flutter_ahj_app/base/values/dimens.dart';

import '../../../base/basewidget/view/BaseWidget.dart';

class ResetPswdView extends BaseStatefulWidget {
  var map; // phone phoneCaptcha
  ResetPswdView(this.map);
  @override
  State<StatefulWidget> initState() {
    // TODO: implement initState
    return ResetPswdViewState();
  }
}

class ResetPswdViewState extends BaseState<AccountViewModel, ResetPswdView> {
  TextEditingController _oldPwdCtr = TextEditingController();
  TextEditingController _newPWDCtr = TextEditingController();

  ///新密码
  ///
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget initWidget() {
    // TODO: implement initWidget
    return new Scaffold(
      backgroundColor: SQColor.theme_Bg_color,
      appBar: new AppBar(
        centerTitle: true,
        elevation: 0,
        backgroundColor: SQColor.theme_color,
        title: JText(
          '重置密码',
          color: SQColor.white,
          fontSize: 18,
        ),
      ),
      body: _bodyView(),
    );
  }

  _bodyView() {
    return SingleChildScrollView(
      child: Column(
        children: <Widget>[
          _topContent(),
        ],
      ),
    );
  }

  _topContent() {
    return Column(
      children: [
        SizedBox(
          height: Dimens.dimens14,
        ),
        Container(
          margin:
              EdgeInsets.only(left: Dimens.dimens14, right: Dimens.dimens14),
          decoration: BoxDecoration(
            color: SQColor.white,
            borderRadius: BorderRadius.all(Radius.circular(4.0)),
          ),
          child: Column(
            children: [
              Row(
                children: [
                  SizedBox(
                    width: Dimens.dimens14,
                  ),
                  Text("*", style: TextStyle(color: SQColor.red)),
                  Expanded(
                    child: _oldPwdView(),
                  )
                ],
              ),
              Container(
                height: 1.0,
                color: SQColor.lightGray1,
                margin: EdgeInsets.only(
                    left: Dimens.dimens16, right: Dimens.dimens16),
              ),
              Row(
                children: [
                  SizedBox(
                    width: Dimens.dimens14,
                  ),
                  Text("*", style: TextStyle(color: SQColor.red)),
                  Expanded(
                    child: _newPwdView(),
                  )
                ],
              ),
            ],
          ),
        ),
        _sureBtnContent()
      ],
    );
  }

  _oldPwdView() {
    return

        ///
        Container(
      alignment: Alignment.center,
      child: TextField(
          controller: _oldPwdCtr,
          inputFormatters: [
          LengthLimitingTextInputFormatter(15),
          FilteringTextInputFormatter.deny(RegExp(" ")),
        ],
          decoration: InputDecoration(
            icon: JText(
              '新密码:',
              color: SQColor.black_333333,
            ),
            hintText: '6~15位，字母、数字、字符任意两种',
            hintStyle: TextStyle(fontSize: 13, color: Color(0xffBFBFBF)),
            border: InputBorder.none,
          )),
      height: 54,
      padding: EdgeInsets.only(right: Dimens.dimens14),
    );
  }

  _newPwdView() {
    return Container(
      alignment: Alignment.center,
      child: TextField(
          controller: _newPWDCtr,
          inputFormatters: [
          LengthLimitingTextInputFormatter(15),
          FilteringTextInputFormatter.deny(RegExp(" ")),
        ],
          decoration: InputDecoration(
            icon: JText(
              '确认新密码:',
              color: SQColor.black_333333,
            ),
            hintText: '6~15位，字母、数字、字符任意两种',
            hintStyle: TextStyle(fontSize: 13, color: Color(0xffBFBFBF)),
            border: InputBorder.none,
          )),
      height: 54,
      padding: EdgeInsets.only(right: Dimens.dimens14),
    );
  }

  _sureBtnContent() {
    return InkWell(
      child: Container(
        margin: EdgeInsets.fromLTRB(14, 14, 14, 0),
        decoration: BoxDecoration(
          color: SQColor.theme_color,
          borderRadius: BorderRadius.all(Radius.circular(4.0)),
        ),
        child: Center(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              JText(
                '确定',
                color: SQColor.white,
                fontSize: 16,
                fontWeight: FontWeight.w500,
              )
            ],
          ),
        ),
        height: 48,
      ),
      onTap: () {
        _done();
      },
    );
  }

  _done() {
    FocusScope.of(context).requestFocus(FocusNode());
    if (_oldPwdCtr.text.isEmpty) {
      ToastUtils.showToast('请填写新密码');
      return;
    }
    if (_oldPwdCtr.text != _newPWDCtr.text) {
      ToastUtils.showToast('两次密码输入不一致');
      return;
    }
    widget.map['password'] = _oldPwdCtr.text;
    widget.map['confirmPassword'] = _newPWDCtr.text;
    viewModel.resetPassword(widget.map);
    viewModel.resetPasswordStream().listen((event) {
      if (event['code'] == '0') {
        ToastUtils.showToast('密码重置成功');
        _loginOut();
      } else {
        ToastUtils.showToast(event['msg']);
      }
    }, onError: (e) {
      print(e);
    });
  }

  _loginOut() {
    viewModel.loginOut();
    viewModel.loginOutStream().listen((event) {
      if (event['code'] == '0') {
        UserInfoUtils.setUserToken('');
        Navigator.pushNamedAndRemoveUntil(context, "/LoginPageView", (route) => false);
      } else {
        ToastUtils.showToast(event['msg']);
      }
    }, onError: (e) {
      print(e);
    });
  }
}
